package neiltwist.cfd.lightweightakka;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

public class CopyOfCFDOld {
	
	private static final Random randomGenerator = new Random();
	
	// Default values. Lets use these for now...
	private int N=512; // grid resoultion
	private float timeStep=0.2f;
	private float diffusionRate=0.0f;
	private float viscosity=0.0f;
	
	//TODO: Should try these as two dimensional arrays and compare the speed...
	float[] u, v, u_prev, v_prev;
	float[] dens, dens_prev; 
	
	List<List<Float>> neil;
	
	public CopyOfCFDOld(){
		int size = (N+2) * (N+2);
		u = new float[size];
		v = new float[size];
		u_prev = new float[size];
		v_prev = new float[size];
		
		dens = new float[size];
		dens_prev = new float[size];
		
//		neil = new ArrayList<List<Float>>(N);
		
		neil = Lists.newArrayListWithCapacity(N);
		
		// initialise the array
		for (int x=0 ; x<N; x++){
			List<Float> thisList=Lists.newArrayListWithCapacity(N);
			for (int y=0 ; y<N; y++){
//				System.out.println(x+" "+y);
				thisList.add(y, Float.valueOf(0f));
			}
			neil.add(x,thisList);
    	}
	}
	
	//Utilities
	private int getIndex(int i, int j){
		return (i + (N+2)*j);
	}
	
	//External calls
	public void addSource(int N, float[] x, float[] s, float dt){
		for ( int i = 0; i < ((N+2)*(N+2)); i++ ) {
			x[i] += dt*s[i];
		}
	}
	
	
	public void step(){
		stepVelocity();
		stepDensity();
		setU();
	}
	
	private void setU() {
		
		/**
		 * Quite a nice little example of how to use the functions
		 */
		List<List<Float>> newXList =
				Lists.transform(neil, new Function<List<Float>, List<Float>>() {
					@Override
					public List<Float> apply(List<Float> arg0) {
						List<Float> newYList = Lists.transform(arg0, new Function<Float, Float>() {
							@Override
							public Float apply(Float arg0) {
								return randomGenerator.nextFloat();
							}
						});
						return Lists.newArrayList(newYList);
					}
				});

		neil = Lists.newArrayList(newXList);
		
		
		/*for (int x=0 ; x<N; x++){
			List<Float> thisList = neil.get(x);

			thisList.clear();			
			for (int y=0 ; y<N; y++){
//    			u[getIndex(x, y)] = randomGenerator.nextFloat();
    			thisList.add(y,randomGenerator.nextFloat()); 
    		}
    	}*/
	}
	
	private void stepVelocity() {
		
	}
	
	private void stepDensity() {
		
	}
	
	
	
	public double getPixelValue(int x, int y) {
//		return u[getIndex(x, y)];
		return neil.get(x).get(y);
	}

}
